package com.javasm.controller.MisMannageController;

import com.alibaba.fastjson.JSON;
import com.javasm.entity.MisMannageEntity.AreaInfo;
import com.javasm.entity.MisMannageEntity.ReturnCode;
import com.javasm.entity.MisMannageEntity.ReturnEntity;
import com.javasm.service.MisMannageService.AreaService;
import com.javasm.service.MisMannageService.impl.AreaServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * @author:JAVASM
 * @description:
 * @date:2022/5/23 14:34
 * @version:0.1
 * @since:jdk11
 */
@WebServlet("/area/*")
public class AreaServlet extends BaseServlet {

    protected void queryArea(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String queryAreaPidStr = req.getParameter("queryAreaPid");

        Integer queryAreaPid = 0;
        if (queryAreaPidStr!=null && !"".equals(queryAreaPidStr)){
            queryAreaPid = Integer.parseInt(queryAreaPidStr);
        }

        AreaService as = new AreaServiceImpl();
        List<AreaInfo> allAddr = as.getAllArea(queryAreaPid);

        ReturnEntity re = new ReturnEntity();
        if (allAddr.size()>0){
            re.setRetCode(ReturnCode.QUERY_OK.getCode());
            re.setRetMsg(ReturnCode.QUERY_OK.getMsg());
            re.setRetData(allAddr);
        }else{
            re.setRetCode(ReturnCode.QUERY_FAILED.getCode());
            re.setRetMsg(ReturnCode.QUERY_FAILED.getMsg());
        }

        resp.setContentType("application/json;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        writer.print(JSON.toJSONString(re));
        writer.flush();
        writer.close();
    }
}
